package com.enjoy.xbase.ui.old;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.enjoy.xbase.R;
import com.enjoy.xbase.ui.load.DefaultLoadView;
import com.enjoy.xbase.ui.load.ILoadRefresh;
import com.enjoy.xbase.ui.load.LoadMutual;

import java.util.ArrayList;
import java.util.List;

@Deprecated
public abstract class DataListFragment extends Fragment {
    private LinearLayout rootView;
    public DefaultLoadView loadView;
    private RecyclerView recyclerView;
    private RvAdapter adapter;
    private boolean uiFinish=false;
    private List data=new ArrayList();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView==null){
            rootView= (LinearLayout) inflater.inflate(R.layout.fragment_base_list_data,container,false);
            initView();
            initListener();
            initData();
        }
        return rootView;
    }
    protected void initView(){
        loadView=rootView.findViewById(R.id.v_load);
        loadView.setLoadBackgroundColor(getBackgroundColor());
        recyclerView = rootView.findViewById(R.id.list);
        int column=getColumn();
        if (column<=1){
            //创建LinearLayoutManager 对象 这里使用 LinearLayoutManager 是线性布局的意思
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            //设置RecyclerView 布局
            recyclerView.setLayoutManager(linearLayoutManager);
        }else{
            //布局管理器所需参数，上下文
            GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), column);
            //B 通过布局管理器，可以控制条目排列顺序  true：反向显示  false：正常显示(默认)
            gridLayoutManager.setReverseLayout(false);
            //C 设置RecyclerView显示的方向，是水平还是垂直！！ GridLayoutManager.VERTICAL(默认) false
            gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
            gridLayoutManager.setAutoMeasureEnabled(true);
            recyclerView.setLayoutManager(gridLayoutManager);
        }
        //设置Adapter
        adapter=new RvAdapter();
        recyclerView.setAdapter(adapter);
        initTopView();
    }
    protected int getBackgroundColor(){
        return Color.parseColor("#ffffff");
    }
    protected void setDataViewBackgroundColor(int color){
        if (recyclerView!=null)recyclerView.setBackgroundColor(color);
    }
    protected void initListener(){

        loadView.setLoadRefresh(new ILoadRefresh(){
            @Override
            public void onRefresh() {
                loadView.showData();
            }
        });
    }
    protected void initData(){
        uiFinish=true;
        updateLoadView();
    }
    private void initTopView(){
        View topView=onCreateTopView(rootView);
        if (topView!=null)rootView.addView(topView,0);
    }

    public void setLoadCreateView(LoadMutual loadMutual){
        loadView.setLoadMutual(loadMutual);
    }

    //创建list顶部视图
    protected View onCreateTopView(ViewGroup container){
        return null;
    }
    //列数
    protected int getColumn(){
        return 1;
    }

    public void setData(List data){
        this.data.clear();
        if (data!=null){
            this.data.addAll(data);
        }
        updateLoadView();
    }
    private void updateLoadView(){
        if (!uiFinish){
            return;
        }
        if(data.size()==0){
            loadView.showEmpty();
        }else{
            loadView.showData();
        }
        this.adapter.notifyDataSetChanged();
    }

    public <T> T getData(int position){
        return (T)data.get(position);
    }

    public <T> List<T> getAllData(){
        return data;
    }

    public void removeData(Object object){
        data.remove(object);
        if (data.size()==0){
            loadView.showEmpty();
        }
        adapter.notifyDataSetChanged();
    }

    public void notifyDataSetChanged(){
        adapter.notifyDataSetChanged();
    }

    protected int getRvItemCount() {
        return data.size();
    }

    protected int getRvItemViewType(int position) {
        return 0;
    }

    protected abstract RecyclerView.ViewHolder onCreateRvViewHolder(ViewGroup parent, int viewType);

    protected abstract void onBindRvViewHolder(RecyclerView.ViewHolder holder, int position);

    class RvAdapter extends RecyclerView.Adapter {

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return onCreateRvViewHolder(parent, viewType);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
           onBindRvViewHolder(holder, position);
        }

        @Override
        public int getItemCount() {
            return getRvItemCount();
        }

        @Override
        public int getItemViewType(int position) {
            return getRvItemViewType(position);
        }
    }

}
